package war2100.entity;

import java.util.ArrayList;

import war2100.entity.help.BasicHelpEntity;
import war2100.entity.help.PlayerStartPos;
import war2100.entity.tiles.Tile;

public class Map {
	private Tile[][] map;
	private ArrayList<BasicHelpEntity> entitys;
	private int xSize;
	private int ySize;
	private int currentStartpos;

	public Map(int x, int y, Tile v) {
		this.xSize = x;
		this.ySize = y;
		setCurrentStartpos(1);
		entitys = new ArrayList<BasicHelpEntity>();
		map = new Tile[xSize][ySize];

		for (int i = 0; i < xSize; i++) {
			for (int j = 0; j < ySize; j++) {
				setField(i, j, v);
			}
		}
	}

	public Tile getField(int x, int y) {
		if (x < 0 || x >= xSize || y < 0 || y >= ySize) {
			return null;
		}
		return map[x][y];
	}

	public void setField(int x, int y, Tile object) {
		if (x < 0 || x >= xSize || y < 0 || y >= ySize) {
			throw new IllegalArgumentException("Ungültige Mapangabe!");
		}
		map[x][y] = object;
	}
	
	public ArrayList<PlayerStartPos> getPlayerStartPos() {
		ArrayList<PlayerStartPos> list = new ArrayList<PlayerStartPos>();
		
		for (Entity e : entitys) {
			if (e instanceof PlayerStartPos) {
				list.add((PlayerStartPos) e);
			}
		}
		
		return list;
	}

	public void addColumn(Tile v) {
		Tile[][] oldMap = map;
		map = new Tile[xSize + 1][ySize];
		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				map[x][y] = oldMap[x][y];
			}
		}
		for (int y = 0; y < ySize; y++) {
			map[xSize][y] = v;
		}
		xSize++;
	}

	public void removeColumn() {
		Tile[][] oldMap = map;
		xSize--;
		map = new Tile[xSize][ySize];
		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				map[x][y] = oldMap[x][y];
			}
		}
	}

	public void removeLine() {
		Tile[][] oldMap = map;
		ySize--;
		map = new Tile[xSize][ySize];
		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				map[x][y] = oldMap[x][y];
			}
		}
	}

	public void addLine(Tile v) {
		Tile[][] oldMap = map;
		map = new Tile[xSize][ySize + 1];
		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				map[x][y] = oldMap[x][y];
			}
		}
		for (int x = 0; x < xSize; x++) {
			map[x][ySize] = v;
		}
		ySize++;
	}

	public void addEntity(BasicHelpEntity e) {
		entitys.add(e);
	}

	public void removeEntity(BasicHelpEntity e) {
		entitys.remove(e);
	}

	public int getxSize() {
		return xSize;
	}

	public void setxSize(int xSize) {
		this.xSize = xSize;
	}

	public int getySize() {
		return ySize;
	}

	public void setySize(int ySize) {
		this.ySize = ySize;
	}

	public ArrayList<BasicHelpEntity> getEntitys() {
		return entitys;
	}

	public void setEntitys(ArrayList<BasicHelpEntity> entitys) {
		this.entitys = entitys;
	}

	public int getCurrentStartpos() {
		return currentStartpos;
	}

	public void setCurrentStartpos(int currentStartpos) {
		this.currentStartpos = currentStartpos;
	}
}
